package com.company.quick56.web.bean.base;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.impl.UIEditDataGrid;

import com.company.quick56.web.entity.Pack;
import com.company.quick56.web.service.base.IPackService;
import com.company.quick56.web.util.CommonUtil;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "base.packBean", scope = ManagedBeanScope.SESSION)
public class PackBean implements Serializable {

	/**
	 * Inject the service
	 */
	@ManagedProperty("#{packService}")
	private IPackService service;

	/**
	 * the 'name' property
	 */
	@Bind(id = "nameTf", attribute = "value")
	private String name;

	/**
	 * the 'mnemocode' property
	 */
	@Bind(id = "mnemocodeTf", attribute = "value")
	private String mnemocode;

	/**
	 * the handler of the detail datagrid
	 */
	@Bind(id = "packGrid", attribute = "binding")
	private UIEditDataGrid packGrid;

	/**
	 * the added data of detail datagrid
	 */
	@Bind(id = "packGrid", attribute = "addedData")
	private Pack[] addedData;

	/**
	 * the modified data of detail datagrid
	 */
	@Bind(id = "packGrid", attribute = "modifiedData")
	private Pack[] modifiedData;

	/**
	 * the removed data of detail datagrid
	 */
	@Bind(id = "packGrid", attribute = "removedData")
	private Pack[] removedData;

	/**
	 * the type of the detail datagrid
	 */
	@Bind(id = "packGrid", attribute = "bindBean")
	private Class packBindBean = Pack.class;

	@Action(id = "search")
	public void search() {
		packGrid.reload();
	}

	@Action(id = "add")
	public void add() {
		packGrid.appendRow();
	}

	@Action(id = "delete")
	public void delete() {
		packGrid.remove();
	}

	@Action(id = "export")
	public void export() {
		FacesContext ctx = FacesContext.getCurrentInstance();
		ctx.responseComplete();
		HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
		CommonUtil.generateExcel(response);
		ctx.responseComplete();
	}

	@Action(id = "save")
	public void save() {
		if (addedData != null) {
			for (Pack detail : addedData) {
				service.create(detail);
			}
		}
		if (modifiedData != null) {
			for (Pack detail : modifiedData) {
				service.modify(detail);
			}
		}
		if (removedData != null) {
			for (Pack detail : removedData) {
				service.remove(detail);
			}
		}
		addedData = null;
		modifiedData = null;
		removedData = null;
		packGrid.reload();
	}

	@DataModel(id = "packGrid")
	private List<Pack> getPackGridValues() {
		List<Pack> data = service.find(name, mnemocode);
		return data;
	}

	public UIEditDataGrid getPackGrid() {
		return packGrid;
	}

	public void setPackGrid(UIEditDataGrid packGrid) {
		this.packGrid = packGrid;
	}

}
